package kevin.hbase.util;

import java.time.*;
import java.time.format.DateTimeFormatter;
import java.util.TimeZone;

public class DateTimeUtil {
    public static final String DATETIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
    public static final String DATE_FORMAT = "yyyy-MM-dd";
    public static final String TIME_FORMAT = "HH:mm:ss SSS";

    public static final String FULL_DATETIME_FORMAT = "yyyy-MM-dd'T'HH:mm:ss.SSSSSS";
    public static final String DATETIME_FORMAT_SIMPLE = "yyMMddHHmmss";
    public static final String LOCATION_DATETIME_FORMAT = "ddMMyyHHmmss";
    public static final String LOCATION_DATETIME_FORMAT_2 = "ddMMyyHHmmss.SS";
    public static final String LOCATION_DATETIME_FORMAT_3 = "ddMMyyHHmmss.SSS";

    public static LocalDateTime parseStringToDateTime(String time, String format) {
        DateTimeFormatter df = DateTimeFormatter.ofPattern(format);
        return LocalDateTime.parse(time, df);
    }

    public static long getTimestampOfDateTime(LocalDateTime localDateTime) {
        ZoneId zone = ZoneId.systemDefault();
        Instant instant = localDateTime.atZone(zone).toInstant();
        return instant.toEpochMilli();
    }

    public static long getTimestampOfDate(LocalDate localDate) {
        ZoneId zone = ZoneId.systemDefault();
        LocalDateTime localDateTime = LocalDateTime.of(localDate, LocalTime.MIN);
        Instant instant = localDateTime.atZone(zone).toInstant();
        return instant.toEpochMilli();
    }

    public static LocalDateTime getDateTimeOfTimestamp(long timestamp) {
        Instant instant = Instant.ofEpochMilli(timestamp);
        ZoneId zone = ZoneId.systemDefault();
        return LocalDateTime.ofInstant(instant, zone);
    }

    public static long getTimestampOfDateTimeGMT8(LocalDateTime localDateTime) {
        ZoneId zone = TimeZone.getTimeZone("GMT+8").toZoneId();
        Instant instant = localDateTime.atZone(zone).toInstant();
        return instant.toEpochMilli();
    }

    public static long getTimestampOfDateTimeGMT(LocalDateTime localDateTime) {
        ZoneId zone = TimeZone.getTimeZone("GMT").toZoneId();
        Instant instant = localDateTime.atZone(zone).toInstant();
        return instant.toEpochMilli();
    }

    public static String getDateTimeAsString(LocalDateTime localDateTime, String format) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(format);
        return localDateTime.format(formatter);
    }

    public static String getDateTimeGMTAsString(LocalDateTime localDateTime, String format) {
        ZonedDateTime zonedDateTime = ZonedDateTime.of(localDateTime, ZoneId.of("GMT+8"));
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(format);
        return formatter.format(zonedDateTime.withZoneSameInstant(ZoneId.of("GMT")));
    }

    public static String getDateTimeGMT8AsString(long timestamp, String format) {
        Instant instant = Instant.ofEpochMilli(timestamp);
        //ZoneId zone = ZoneId.systemDefault();
        ZoneId zone = TimeZone.getTimeZone("GMT+8").toZoneId();
        LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, zone);
        return getDateTimeAsString(localDateTime, format);
    }

    public static String getCurDate() {
        return getDateTimeAsString(LocalDateTime.now(), DATE_FORMAT);
    }

    public static String getCurTime() {
        return getDateTimeAsString(LocalDateTime.now(), TIME_FORMAT);
    }

    public static String getYesterday() {
        return getYesterday(null);
    }

    public static String getYesterday(String format) {
        if (format == null) {
            format = DATE_FORMAT;
        }
        LocalDateTime minusDay = LocalDateTime.now().minusDays(1);
        return getDateTimeAsString(minusDay, format);
    }

    public static String transferZone(String dateTime, String format, String fromZone, String toZone) {
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(format);
        LocalDateTime localDateTime = LocalDateTime.parse(dateTime, dateTimeFormatter);
        LocalDateTime toLocalDateTime = localDateTime.atZone(ZoneId.of(fromZone)).withZoneSameInstant(ZoneId.of(toZone)).toLocalDateTime();
        return toLocalDateTime.format(dateTimeFormatter);
    }

    public static void main(String[] args) {
        System.out.println(getCurDate());
        System.out.println(getYesterday());
        System.out.println(getCurTime());
        System.out.println(DateTimeUtil.getDateTimeAsString(LocalDateTime.now(), "HHmmssddMMyy"));
        System.out.println(DateTimeUtil.getDateTimeGMTAsString(LocalDateTime.now(), "HHmmssddMMyy"));
        System.out.println(transferZone("2023-06-27 16:06:23", DATETIME_FORMAT, "GMT", "GMT+8"));
        System.out.println(DateTimeUtil.getDateTimeGMTAsString(LocalDateTime.now(), "HHmmssddMMyy"));
    }

}
